﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using fivescomplement.Models;

namespace fivescomplement.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            if (Convert.ToInt32(Session["Type"]) == 0)
            {
                if (User.Identity.IsAuthenticated == true)
                {
                    int test = Convert.ToInt32(Session["Type"]);
                    RedirectToAction("LogOff", "Account");
                }
            }
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }

 

        [HttpPost]
        public ActionResult Survey(FormCollection form, string id)
        {
            for (int i = 0; i < form.Count/4; i++)
            {
                int CourseNumber = Convert.ToInt32(form["CourseID" + i.ToString()]);
                int QuestionID = Convert.ToInt32(form["QuestionID" + i.ToString()]);
                int QuestionTypeID = Convert.ToInt32(form["QuestionTypeID" + i.ToString()]);
                int resultvalue = 0;
                string resultcomment = "";
                if (QuestionTypeID == 1)
                {
                    resultvalue = Convert.ToInt32(form["5Scale" + i.ToString()]);
                }
                else if (QuestionTypeID == 2)
                {
                    resultvalue = Convert.ToInt32(form["T/F" + i.ToString()]);
                }
                else if (QuestionTypeID == 3)
                {
                    resultcomment = form["Comments" + i.ToString()].ToString();
                }

                using (var context = new C335263_teacherharvestEntities())
                {
                    //insert CourseNumber, QuestionID, resultvalue, resultcomment into AnsweredQuestions
                    AnsweredQuestions newQuestions = AnsweredQuestions.CreateAnsweredQuestions(CourseNumber, QuestionID, resultvalue, resultcomment);
                    context.AnsweredQuestions.AddObject(newQuestions);
                    context.SaveChanges();
                }
            }

            using (var context = new C335263_teacherharvestEntities())
            {
                //update student as completed
                var user = from u in context.Students
                           where u.Reference == id
                           select u;

                user.First().Completed = true;
                context.SaveChanges();
            }

            return RedirectToAction("Completed", "Home");
        }

        public ActionResult Survey(string id)
        {
            //var EmpItems = new GetQuestions_Result1();
            using (var database = new C335263_teacherharvestEntities())
            {
                    var studentuser = database.GetQuestionsStudent(id);
                    
                    return View(studentuser.ToList());

                //set something that tells the user that they can not take the 

                //Usefull code for getting and looping through query results. Just need a different Stored Procedure
                //var EmpItems = from e in studentuser
                //               select new ClassQuestions
                //               {
                //                   InstitutionID = e.InstitutionID,
                //                   CourseID = e.CourseID,
                //                   Completed = e.Completed,
                //                   QuestionTypeID = e.QuestionTypeID,
                //                   Question = e.Question
                //               };


                //foreach (ClassQuestions question in EmpItems)
                //{
                //    int? QuestionTypeID = question.QuestionTypeID;
                //    string newquestion = question.Question;

                //    if (QuestionTypeID == 1)
                //    {


                //    }
                //}
            }          
        }


        public ActionResult Completed()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }


        
    }
}
